using UnityEngine;
using System.Collections;

public class NegativeBlinking : MonoBehaviour 
{
	Color mBaseColor;
	Color mTargetColor;
	
	bool toDestroy = false;
	
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(toDestroy)
		{
			renderer.material.color = mBaseColor;
			Destroy(this);
		}
		else
		{
			renderer.material.color = Color.Lerp(mBaseColor, mTargetColor, Mathf.PingPong(Time.time * 8, 1));
		}
	}
	
	public void OnDestroy()
	{
		renderer.material.color = mBaseColor;
	}
	
	public void StartBlinking(float pSeconds)
	{
		mBaseColor = renderer.material.color;
		mTargetColor = Color.white - mBaseColor;
		
		StartCoroutine(BlinkCoroutine(pSeconds));
	}
	
	public IEnumerator BlinkCoroutine(float pSeconds)
	{
		yield return new WaitForSeconds(pSeconds);
		
		toDestroy = true;
	}
}
